package com.wswy.wzcx.widget;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Space;
import android.widget.TextView;

import com.wswy.wzcx.R;
import com.wswy.wzcx.ui.other.AndroidUtilities;
import com.wswy.wzcx.ui.other.LayoutHelper;

/**
 * Created by wangduheng26 on 2018/1/22.
 * wangduheng26@gmail.com
 */

public class VerCodeView extends FrameLayout {
    private EditText editText;
    private TextView[] textViews;
    private static int MAX = 4;
    private String inputContent;

    public VerCodeView(Context context) {
        super(context);
        init();
    }

    @Override
    public View findFocus() {
        return editText.findFocus();
    }

    private void init() {
        Context context = getContext();
        setMinimumHeight(AndroidUtilities.dp(80));
        LinearLayout layout = new LinearLayout(context);
        layout.setOrientation(LinearLayout.HORIZONTAL);
        layout.setPadding(AndroidUtilities.dp(14), 0, AndroidUtilities.dp(14), 0);
        textViews = new TextView[MAX];

        for (int i = 0; i < MAX; i++) {
            Space space = new Space(context);
            layout.addView(space, LayoutHelper.createLinear(18, LayoutHelper.MATCH_PARENT ));
            textViews[i] = new TextView(context);
            textViews[i].setGravity(Gravity.CENTER);
            textViews[i].setBackgroundResource(R.drawable.input_bg);
            textViews[i].setMinWidth(AndroidUtilities.dp(60));
            layout.addView(textViews[i], LayoutHelper.createLinear(0, LayoutHelper.MATCH_PARENT,1f));
        }
        Space space = new Space(context);
        layout.addView(space, LayoutHelper.createLinear(18, LayoutHelper.MATCH_PARENT));
        addView(layout, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
        editText = new MyEditText(context);
        editText.setCursorVisible(false);//隐藏光标
        setEditTextListener();
        addView(editText, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
    }

    private void setEditTextListener() {
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                inputContent = editText.getText().toString();

                if (inputCompleteListener != null) {
                    if (inputContent.length() >= MAX) {
                        inputCompleteListener.inputComplete();
                    } else {
                        inputCompleteListener.invalidContent();
                    }
                }

                for (int i = 0; i < MAX; i++) {
                    if (i < inputContent.length()) {
                        textViews[i].setText(String.valueOf(inputContent.charAt(i)));
                    } else {
                        textViews[i].setText("");
                    }
                }
            }
        });
    }

    private InputCompleteListener inputCompleteListener;

    public void setInputCompleteListener(InputCompleteListener inputCompleteListener) {
        this.inputCompleteListener = inputCompleteListener;
    }

    public interface InputCompleteListener {

        void inputComplete();

        void invalidContent();
    }

    public String getEditContent() {
        return inputContent;
    }

    class MyEditText extends AppCompatEditText {
        private long lastTime = 0;

        public MyEditText(Context context) {
            super(context);
            setLongClickable(false);
            setBackgroundColor(Color.TRANSPARENT);
            setTextColor(Color.TRANSPARENT);
            setMaxLines(MAX);
            setInputType(InputType.TYPE_CLASS_NUMBER);
        }

        @Override
        protected void onSelectionChanged(int selStart, int selEnd) {
            super.onSelectionChanged(selStart, selEnd);
            //把光标位置固定在最末
            this.setSelection(this.getText().length());
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            //屏蔽双击事件
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    long currentTime = System.currentTimeMillis();
                    if (currentTime - lastTime < 500) {
                        lastTime = currentTime;
                        return true;
                    } else {
                        lastTime = currentTime;
                    }
                    break;
            }
            return super.onTouchEvent(event);
        }

    }
}
